package com.xt.purchasingsystem.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xt.purchasingsystem.dto.SysMenusDto;
import com.xt.purchasingsystem.mapper.SysMenusMapper;
import com.xt.purchasingsystem.model.SysMenus;
import com.xt.purchasingsystem.service.SysMenusService;
import com.xt.purchasingsystem.utils.MySQLPageBean;
import com.xt.purchasingsystem.utils.Result;
import com.xt.purchasingsystem.utils.ResultFactory;
import com.xt.purchasingsystem.utils.SysMenuUtil;
import com.xt.purchasingsystem.vo.Menu;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 菜单信息 服务实现类
 * </p>
 *
 * @author yhp
 * @since 2020-05-18
 */
@Service
public class SysMenusServiceImpl extends ServiceImpl<SysMenusMapper, SysMenus> implements SysMenusService {
    @Autowired
    private SysMenusMapper sysMenusMapper;


    public Result queryAllMenus_fallback() {
        return ResultFactory.buildErrorResult("菜单服务繁忙,请稍后再试");
    }
    @Override
//    @HystrixCommand(fallbackMethod = "queryAllMenus_fallback", commandProperties = {
//            @HystrixProperty(name = "circuitBreaker.enabled", value = "true"),// 是否开启断路器
//            @HystrixProperty(name = "circuitBreaker.requestVolumeThreshold", value = "10"),// 请求次数
//            @HystrixProperty(name = "circuitBreaker.sleepWindowInMilliseconds", value = "10000"), // 时间窗口期
//            @HystrixProperty(name = "circuitBreaker.errorThresholdPercentage", value = "60"),// 失败率达到多少后跳闸
//    })
    public Result queryAllMenus() {
        List<Menu> sysMenus = sysMenusMapper.queryAllUsableMenus();
        List<Menu> menuTree = SysMenuUtil.getMenuTree(sysMenus);
        return ResultFactory.buildSuccessResult("查询成功", menuTree);
    }
    public Result querySysMenusBySysMenusDto_fallback(SysMenusDto sysMenusDto) {
        return ResultFactory.buildErrorResult("菜单服务繁忙,请稍后再试");
    }
    @Override
//    @HystrixCommand(fallbackMethod = "querySysMenusBySysMenusDto_fallback", commandProperties = {
//            @HystrixProperty(name = "circuitBreaker.enabled", value = "true"),// 是否开启断路器
//            @HystrixProperty(name = "circuitBreaker.requestVolumeThreshold", value = "10"),// 请求次数
//            @HystrixProperty(name = "circuitBreaker.sleepWindowInMilliseconds", value = "10000"), // 时间窗口期
//            @HystrixProperty(name = "circuitBreaker.errorThresholdPercentage", value = "60"),// 失败率达到多少后跳闸
//    })
    public Result querySysMenusBySysMenusDto(SysMenusDto sysMenusDto) {
        //return null;
        List<Menu> sysMenus = sysMenusMapper.queryAllMenus();
        List<Menu> menuTree = SysMenuUtil.getMenuTree(sysMenus);
        List<Menu> menusList = new ArrayList<Menu>();

        if (sysMenusDto.getSysMenus().getName().length() <= 0 && sysMenusDto.getSysMenus().getStatus() == null) {
            // name,状态 为空
            //分页查询所有
            MySQLPageBean mySqlPageBean = new MySQLPageBean(sysMenusDto.getPageSize(),
                    sysMenusDto.getCurrNo(), menuTree.size());

            for (int i =  mySqlPageBean.getStart(); i <= sysMenusDto.getPageSize() * sysMenusDto.getCurrNo() - 1; i++) {
                if (i > menuTree.size() - 1) {
                    break;
                } else {
                    menusList.add(menuTree.get(i));
                }
            }
            mySqlPageBean.setPageList(menusList);
            return ResultFactory.buildSuccessResult("查询成功", mySqlPageBean);


        } else {

            if (sysMenusDto.getSysMenus().getName().length() > 0 && sysMenusDto.getSysMenus().getStatus() != null) {

                List<Menu> menusList1 =
                        menuTree.stream().filter(menu ->
                                menu.getName().indexOf(sysMenusDto.getSysMenus().getName()) != -1 && menu.getStatus().equals(sysMenusDto.getSysMenus().getStatus()) )
                                .collect(Collectors.toList());

                MySQLPageBean mySqlPageBean = new MySQLPageBean(sysMenusDto.getPageSize(),
                        sysMenusDto.getCurrNo(), menusList1.size());
                if (menusList1.size() > 0) {
                    List<Menu> list = new ArrayList<Menu>();
                    for (int i = mySqlPageBean.getStart(); i <= sysMenusDto.getPageSize() * sysMenusDto.getCurrNo() - 1; i++) {
                        if (i > menusList1.size() - 1) {
                            break;
                        } else {
                            list.add(menusList1.get(i));
                        }
                    }
                    mySqlPageBean.setPageList(list);
                    return ResultFactory.buildSuccessResult("查询成功", mySqlPageBean);

                } else {
                    mySqlPageBean.setPageList(menusList);
                    return ResultFactory.buildSuccessResult("查询成功", mySqlPageBean);
                }

            }

            //name 不为空 状态不为空
            if (sysMenusDto.getSysMenus().getName().length() > 0 && sysMenusDto.getSysMenus().getStatus() == null) {
                //name 不为空 状态为空
                for (Menu menus : menuTree) {
                    if (menus.getName().indexOf(sysMenusDto.getSysMenus().getName()) != -1) {
                        menusList.add(menus);
                    }
                }
                MySQLPageBean mySqlPageBean = new MySQLPageBean(sysMenusDto.getPageSize(),
                        sysMenusDto.getCurrNo(), menusList.size());
                if (menusList.size() > 0) {
                    List<Menu> list = new ArrayList<Menu>();
                    for (int i = mySqlPageBean.getStart(); i <= sysMenusDto.getPageSize() * sysMenusDto.getCurrNo() - 1; i++) {
                        if (i > menusList.size() - 1) {
                            break;
                        } else {
                            list.add(menusList.get(i));
                        }
                    }
                    mySqlPageBean.setPageList(list);
                    return ResultFactory.buildSuccessResult("查询成功", mySqlPageBean);

                } else {
                    mySqlPageBean.setPageList(menusList);
                    return ResultFactory.buildSuccessResult("查询成功", mySqlPageBean);
                }
            }

            if (sysMenusDto.getSysMenus().getName().length() <= 0 && sysMenusDto.getSysMenus().getStatus() != null) {

                List<Menu> menusList1 =
                        menuTree.stream().filter(menu -> menu.getStatus().equals( sysMenusDto.getSysMenus().getStatus())).collect(Collectors.toList());
                MySQLPageBean mySqlPageBean = new MySQLPageBean(sysMenusDto.getPageSize(),
                        sysMenusDto.getCurrNo(), menusList1.size());
                if (menusList1.size() > 0) {
                    List<Menu> list = new ArrayList<Menu>();
                    for (int i = mySqlPageBean.getStart(); i <= sysMenusDto.getPageSize() * sysMenusDto.getCurrNo() - 1; i++) {
                        if (i > menusList1.size() - 1) {
                            break;
                        } else {
                            list.add(menusList1.get(i));
                        }
                    }
                    mySqlPageBean.setPageList(list);
                    return ResultFactory.buildSuccessResult("查询成功", mySqlPageBean);

                } else {
                    mySqlPageBean.setPageList(menusList1);
                    return ResultFactory.buildSuccessResult("查询成功", mySqlPageBean);
                }


            }

            return null;

        }

    }
    public Result updateMenurStatusById_fallback(String menuId, Integer status) {
        return ResultFactory.buildErrorResult("菜单服务繁忙,请稍后再试");
    }
    @Override
//    @HystrixCommand(fallbackMethod = "updateMenurStatusById_fallback", commandProperties = {
//            @HystrixProperty(name = "circuitBreaker.enabled", value = "true"),// 是否开启断路器
//            @HystrixProperty(name = "circuitBreaker.requestVolumeThreshold", value = "10"),// 请求次数
//            @HystrixProperty(name = "circuitBreaker.sleepWindowInMilliseconds", value = "10000"), // 时间窗口期
//            @HystrixProperty(name = "circuitBreaker.errorThresholdPercentage", value = "60"),// 失败率达到多少后跳闸
//    })
    public Result updateMenurStatusById(String menuId, Integer status) {
        int i = sysMenusMapper.updateMenurStatusById(menuId, status);
        if (i == 1) {
            return ResultFactory.buildSuccessResult("修改成功");
        } else {
            return ResultFactory.buildFailResult("修改失败");
        }
    }
}
